package com.atguigu.tingshu.order.pattern.strategy;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.order.pattern.AbstractTradeOrderStrategy;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品类别为声音：策略实现类
 * @author: atguigu
 * @create: 2025-03-25 16:24
 */
@Component("1002")
public class TrackTradeOrderStrategy extends AbstractTradeOrderStrategy {

    @Autowired
    private AlbumFeignClient albumFeignClient;
    /**
     * 处理结算商品类别为：声音
     * @param tradeVo
     * @return
     */
    @Override
    public void handleTrade(TradeVo tradeVo, OrderInfoVo orderInfoVo) {
        //4.处理购买商品类别：声音
        //4.1 远程调用"专辑服务"获取待结算声音列表
        Long trackId = tradeVo.getItemId();
        List<TrackInfo> waitBuyTrackList = albumFeignClient.getWaitBuyTrackList(trackId, tradeVo.getTrackCount()).getData();
        Assert.notNull(waitBuyTrackList, "不存在待结算商品");

        //4.2 远程调用"专辑服务"查询专辑信息，得到声音单价
        Long albumId = waitBuyTrackList.get(0).getAlbumId();
        AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
        Assert.notNull(albumInfo, "声音所属专辑:{}不存在", albumId);
        BigDecimal price = albumInfo.getPrice();

        //4.3 计算相关价格 声音不支持折扣
        BigDecimal originalAmount = price.multiply(BigDecimal.valueOf(tradeVo.getTrackCount()));
        BigDecimal orderAmount = originalAmount;
        BigDecimal derateAmount = new BigDecimal("0.00");
        //4.4 封装订单明细列表
        List<OrderDetailVo> orderDetailVoList = waitBuyTrackList
                .stream()
                .map(trackInfo -> {
                    OrderDetailVo orderDetailVo = new OrderDetailVo();
                    orderDetailVo.setItemId(trackInfo.getId());
                    orderDetailVo.setItemName("声音：" + trackInfo.getTrackTitle());
                    orderDetailVo.setItemUrl(trackInfo.getCoverUrl());
                    orderDetailVo.setItemPrice(price);
                    return orderDetailVo;
                }).collect(Collectors.toList());
        //4.5 即使声音无优惠，也要封装一个订单优惠列表 否则验签会失败
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();

        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        //5.2 为商品列表赋值
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        //5.3 为订单VO其他杂项赋值：购买商品类别
        orderInfoVo.setItemType(tradeVo.getItemType());
        //5.4 流水号、时间戳及签名
        this.generateTradeNo(orderInfoVo);
        this.generateSign(orderInfoVo);
    }
}
